﻿using Authority.Model;
using AutoMapper.DTO;
using Common.Library;
using Dapper.Library;
using Dapper.MsSql.Library;
using Dapper.MySql.Library;
using Sql.DataBase;
using System;
using System.Collections.Generic;
using System.Linq;
using Tool.Model;

namespace Authority.Logic
{
    /// <summary>
    /// 岗位功能管理逻辑
    /// </summary>
    public class SysPostServiceImpl : ISysPostService
    {

        /// <summary>
        /// 分页获取所有的可操作信息
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<SysPostEntity>> LoadSysPostList(ParametersInfo<string> inputInfo)
        {
            var resultInfo = new ResultJsonInfo<List<SysPostEntity>>();
            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
            //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {
                var result = con.QuerySet<SysPostEntity>().WithNoLock();
                if (inputInfo.parameters.IsNotNullOrEmpty())
                {
                    result = result.AndWhere(p => p.sName.Contains(inputInfo.parameters));
                }

                var listInfo = result.OrderBy(inputInfo.order == "asc" ? OrderByType.Asc : OrderByType.Desc, inputInfo.field)
                    .PageList(inputInfo.page, inputInfo.limit);

                if (listInfo.Data.IsNotNull())
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = listInfo.Data.ToList();
                    resultInfo.Count = listInfo.Total;
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取所有的可操作信息
        /// </summary>
        /// <returns></returns>
        public ResultJsonInfo<List<SelectListInfo>> LoadSysPostAllList()
        {
            var resultInfo = new ResultJsonInfo<List<SelectListInfo>>();
            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
            //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {
                var result = con.QuerySet<SysPostEntity>().WithNoLock().Multiple();

                if (result.Count>0)
                {
                    var info = result.MapToList<SelectListInfo>();
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = info;
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }


        /// <summary>
        /// 根据Code获取操作信息
        /// </summary>
        /// <returns></returns>
        public ResultJsonInfo<int> Save(PostOperateRequest inputInfo)
        {
            var resultInfo = new ResultJsonInfo<int>();
            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
            //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {
                var result = 0;
                var sysPost = inputInfo.MapTo<SysPostEntity>();

                if (inputInfo.gCode.IsNotNullOrEmpty())
                {
                    result = con.CommandSet<SysPostEntity>()
                        .IfNotExists(p => p.gCode != inputInfo.gCode && (p.sName == sysPost.sName))
                        .Update(sysPost);
                }
                else
                {
                    sysPost.gCode = GuidHelper.GetGuid();
                    sysPost.dCreateTime = DateTime.Now;
                    result = con.CommandSet<SysPostEntity>()
                            .IfNotExists(p => p.sName == sysPost.sName)
                            .Insert(sysPost);
                }
                if (result > 0)
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = result;
                    resultInfo.Msg = "操作成功！";
                }
                else
                {
                    resultInfo.Msg = "操作失败,请检查岗位是否重复！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="gCodeList"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> Remove(List<string> gCodeList)
        {

            var resultInfo = new ResultJsonInfo<int>();
            using (var con = MySqlDataBase.GetConnection(DatabaseName.AuthorityDB_MySql))
            //using (var con = MsSqlDataBase.GetConnection(DatabaseName.AuthorityDB_MsSql))
            {
                var result = 0;
                if (gCodeList.Count >= 0)
                {
                    var exists = con.QuerySet<SysUserPostEntity>().AndWhere(p => gCodeList.Contains(p.gPostCode)).Exists();
                    if (!exists)
                    {
                        result = con.CommandSet<SysPostEntity>()
                            .AndWhere(a => gCodeList.Contains(a.gCode))
                            .Delete();

                        if (result > 0)
                        {
                            resultInfo.Code = ActionCodes.Success;
                            resultInfo.Data = result;
                            resultInfo.Msg = "操作成功！";
                        }
                        else
                        {
                            resultInfo.Msg = "操作失败！";
                        }
                    }
                    else
                    {
                        resultInfo.Msg = "选中岗位已被用户使用，无法删除！";
                    }
                }
                else
                {
                    resultInfo.Msg = "ID为空，操作失败！";
                }
            }
            return resultInfo;
        }
    }
}
